338E - Optimize - CodeForces Solution


data structures *2600

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ls p << 1
#define rs ls | 1
#define mid ((l + r) >> 1)
using namespace std;
constexpr int N = 150005, M = N * 8;
int n, m, h, a[N], b[N], o;
vector<int> A;
int get(int x) {
    return upper_bound(A.begin(), A.end(), x) - A.begin();
}
int mn[M], add[M];
void pull(int p) {
    mn[p] = min(mn[ls], mn[rs]);
}
void apply(int p, int t) {
    mn[p] += t;
    add[p] += t;
}
void push(int p) {
    if (add[p]) {
        apply(ls, add[p]);
        apply(rs, add[p]);
        add[p] = 0;
    }
}
void change(int x, int y, int z, int p = 1, int l = 1, int r = o) {
    if (x <= l && r <= y) {
        return apply(p, z);
    }
    push(p);
    if (x <= mid) {
        change(x, y, z, ls, l, mid);
    }
    if (mid < y) {
        change(x, y, z, rs, mid + 1, r);
    }
    pull(p);
}
int main() {
    cin >> n >> m >> h;
    for (int i = 1; i <= m; i++) {
        cin >> b[i];
        b[i] = h - b[i];
        A.push_back(b[i]);
    }
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        A.push_back(a[i]);
    }
    sort(A.begin(), A.end());
    A.erase(unique(A.begin(), A.end()), A.end());
    o = A.size();
    for (int i = 1; i <= m; i++) {
        change(get(b[i]), o, 1);
        change(get(a[i]), o, -1);
    }
    int cnt = 0;
    for (int i = 1; i <= n - m + 1; i++) {
        cnt += mn[1] >= 0;
        change(get(a[i]), o, 1);
        if (i + m <= n) {
            change(get(a[i + m]), o, -1);
        }
    }
    cout << cnt << "\n";
    return 0;
}
	    	  			 	  	 		 				 					


Comments

Submit
0 Comments
More Questions

129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem